home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 March: Reference Library / Dev.CD Mar 96 RL / Dev.CD Mar 96 RL.toast / Technical Documentation / develop / develop Issue 25 / develop Issue 25 code / Display Manager Sample / Source / events.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-19  |  11.5 KB  |  573 lines  |  [TEXT/MPCC]

  1. /***********************************************************************
  2. #
  3. #        events.c
  4. #
  5. #        This segment handles the basic event calls.
  6. #
  7. #        Author: Michael Marinkovich
  8. #                Apple Developer Technical Support
  9. #
  10. #
  11. #        Modification History: 
  12. #
  13. #            6/4/95        MWM     Initial coding                     
  14. #            10/12/95    MWM        cleaned up
  15. #
  16. #        Copyright © 1992-95 Apple Computer, Inc., All Rights Reserved
  17. #
  18. #
  19. ***********************************************************************/
  20.  
  21. #include <Events.h>
  22. #include <ToolUtils.h>
  23. #include <Gestalt.h>
  24. #include <OSUtils.h>
  25. #include <Palettes.h>
  26.  
  27. #include "App.h"
  28. #include "Proto.h"
  29.  
  30. extern Boolean        gInBackground;
  31. extern Boolean        gDone;
  32. extern Boolean        gHasAbout;        // have an about box?
  33.  
  34.  
  35. //----------------------------------------------------------------------
  36. //
  37. //    EventLoop - main entry and loop for all event processing
  38. //
  39. //
  40. //----------------------------------------------------------------------
  41.  
  42. void EventLoop(void)
  43. {
  44.     EventRecord        event;
  45.     RgnHandle        cursorRgn;
  46.     Boolean            gotEvent;
  47.         
  48.     gDone = false;
  49.     cursorRgn = NewRgn();
  50.         
  51.     do {
  52.         gotEvent = WaitNextEvent(everyEvent,&event,MyGetSleep(),cursorRgn);
  53.                                     
  54.         if (gotEvent)
  55.             DoEvent(&event);
  56.  
  57.                         
  58.     } while ( !gDone );
  59.         
  60.     DisposeRgn(cursorRgn);
  61. }
  62.  
  63.  
  64. //----------------------------------------------------------------------
  65. //
  66. //    MyGetSleep - return sleep value based upon whether or not the app
  67. //                 is in the background.
  68. //
  69. //----------------------------------------------------------------------
  70.  
  71. short MyGetSleep(void)
  72. {
  73.     short        sleep = 30;
  74.     
  75.     if (gInBackground)
  76.         sleep = 1L;
  77.  
  78.     return sleep;
  79.  
  80. }
  81.  
  82.  
  83. //----------------------------------------------------------------------
  84. //
  85. //    CustomWindowEvent - Handles custom procs assigned to a window. 
  86. //                        Different window kinds can easily have unique event
  87. //                        handlers, ie. floaters, dialogs, documentprocs
  88. //----------------------------------------------------------------------
  89.  
  90. void CustomWindowEvent(short eventType,WindowRef window,void *refCon)
  91. {
  92.     CustomProc        theProc;
  93.     short            kind;
  94.     DocHnd            doc;
  95.     
  96.     kind = GetWindKind(window);
  97.     if (kind < kDocKind || kind > kAboutKind)     // not our window
  98.         return;
  99.  
  100.     doc = (DocHnd)GetWRefCon(window);
  101.     
  102.     if (doc != nil) {
  103.         HLockHi((Handle)doc);
  104.         switch(eventType) {
  105.             case kIdleProc:
  106.                 theProc = (**doc).idleProc;
  107.                 break;
  108.                 
  109.             case kMenuProc:
  110.                 theProc = (**doc).mMenuProc;
  111.                 break;
  112.  
  113.             case kInContentProc:
  114.                 theProc = (**doc).inContentProc;
  115.                 break;
  116.  
  117.             case kInGoAwayProc:
  118.                 theProc = (**doc).inGoAwayProc;
  119.                 break;
  120.                 
  121.             case kInZoomProc:
  122.                 theProc = (**doc).inZoomProc;
  123.                 break;
  124.                 
  125.             case kInGrowProc:
  126.                 theProc = (**doc).inGrowProc;
  127.                 break;
  128.                 
  129.             case kMUpProc:
  130.                 theProc = (**doc).mUpProc;
  131.                 break;
  132.                 
  133.             case kKeyProc:
  134.                 theProc = (**doc).keyProc;
  135.                 break;
  136.             
  137.             case kActivateProc:
  138.                 theProc = (**doc).activateProc;
  139.                 break;
  140.  
  141.             case kUpdateProc:
  142.                 theProc = (**doc).updateProc;
  143.                 break;
  144.  
  145.             default:
  146.                 theProc = nil;
  147.                 break;
  148.         }        
  149.     
  150.         if (theProc != nil) {            
  151.             (*theProc)(window,refCon);
  152.             HUnlock((Handle)doc);
  153.     
  154.         }
  155.     }    
  156.     
  157.     
  158. }
  159.  
  160.  
  161. //----------------------------------------------------------------------
  162. //
  163. //    DoEvent - event dispatcher, called by eventloop
  164. //                
  165. //
  166. //----------------------------------------------------------------------
  167.  
  168. void DoEvent(EventRecord *event)
  169. {
  170.     OSErr            err;
  171.     short            kind;
  172.     long            menuChoice;
  173.     Point            thePoint;
  174.     Boolean            active;
  175.     WindowRef        window;
  176.     
  177.     window = FrontWindow();
  178.  
  179.     switch(event->what) {
  180.         case nullEvent:
  181.             CustomWindowEvent(kIdleProc, window, nil);
  182.             break;
  183.             
  184.         case mouseDown:
  185.             HandleMouseDown(event);
  186.             break;
  187.                             
  188.         case mouseUp:
  189.             break;
  190.                             
  191.         case keyDown:
  192.         case autoKey:
  193.             if (event->modifiers & cmdKey) {                 //    is cmd key down
  194.                 menuChoice = MenuKey(event->message & charCodeMask);
  195.                 kind = GetWindKind(window);
  196.                 if (kind < kDocKind || kind > kFloatKind)             // not our window
  197.                     HandleMenuChoice(window, (void *)&menuChoice);    // default menu
  198.                 else    
  199.                     CustomWindowEvent(kMenuProc, window, (void *)&menuChoice);
  200.             }
  201.             break;
  202.                             
  203.         case activateEvt:
  204.             active = event->modifiers & activeFlag;
  205.             CustomWindowEvent(kActivateProc, (WindowRef)event->message, &active);
  206.             break;
  207.                             
  208.         case updateEvt:
  209.             UpdateWindow((WindowRef)event->message);
  210.             break;
  211.                             
  212.         case diskEvt:
  213.             if (HiWord(event->message) != noErr) {
  214.                 SetPt(&thePoint, 50, 50);
  215.                 err = DIBadMount(thePoint, event->message);
  216.             }
  217.             break;
  218.                             
  219.         case osEvt:
  220.             switch ((event->message >> 24) & 0x0FF) {        
  221.                 case suspendResumeMessage:    
  222.                     gInBackground = event->message & resumeFlag;
  223.                     active = gInBackground;
  224.                     CustomWindowEvent(kActivateProc, FrontWindow(), &active);
  225.                     break;
  226.             }
  227.             break;
  228.     
  229.         case kHighLevelEvent:
  230.             AEProcessAppleEvent(event);
  231.             break;
  232.     }
  233.  
  234. }            
  235.  
  236.  
  237.  
  238. //----------------------------------------------------------------------
  239. //
  240. //    DoIdle - handle Idle events
  241. //                
  242. //
  243. //----------------------------------------------------------------------
  244.  
  245. void DoIdle(WindowRef window, void *refCon)
  246. {
  247.  
  248. }
  249.  
  250.  
  251. //----------------------------------------------------------------------
  252. //
  253. //    HandleMouseDown - 
  254. //                
  255. //
  256. //----------------------------------------------------------------------
  257.  
  258. void HandleMouseDown(EventRecord *event)
  259. {
  260.     long            menuChoice;
  261.     short            thePart;
  262.     short            kind;
  263.     WindowRef        window;
  264.         
  265.  
  266.     thePart = FindWindow(event->where,&window);
  267.         
  268.     switch(thePart) {
  269.         case inMenuBar:
  270.             menuChoice = MenuSelect(event->where);
  271.             window = FrontWindow();
  272.             kind = GetWindKind(window);
  273.             if (kind < kDocKind || kind > kAboutKind)             // not our window
  274.                 HandleMenuChoice(window, (void *)&menuChoice);    // default menu
  275.             else    
  276.                 CustomWindowEvent(kMenuProc, window, (void *)&menuChoice);
  277.             break;
  278.  
  279.         case inContent:
  280.             if (window != FrontWindow())
  281.                 SelectWindow(window);
  282.             else
  283.                 CustomWindowEvent(kInContentProc, window, &event->where);
  284.     
  285.             break;
  286.  
  287.         case inSysWindow:
  288.             SystemClick(event,window);
  289.             break;
  290.                                                 
  291.         case inDrag:
  292.             if (window != FrontWindow())
  293.                 SelectWindow(window);
  294.             DragWindow(window, event->where,&qd.screenBits.bounds);
  295.             break;
  296.                         
  297.         case inGoAway:
  298.             if (TrackGoAway(window, event->where))
  299.                 RemoveWindow(window);
  300.             break;
  301.                         
  302.         case inZoomIn:
  303.         case inZoomOut:
  304.             if (TrackBox(window,event->where,thePart)) 
  305.                 CustomWindowEvent(kInZoomProc, window,&thePart);
  306.             break;
  307.                         
  308.         case inGrow:
  309.             CustomWindowEvent(kInGrowProc, window, &event->where);
  310.             break;
  311.     }
  312.     
  313. }
  314.  
  315.  
  316. //----------------------------------------------------------------------
  317. //
  318. //    HandleMenuChoice - 
  319. //                
  320. //
  321. //----------------------------------------------------------------------
  322.  
  323. void HandleMenuChoice(WindowRef window, void *refCon)
  324. {
  325.     long         menuChoice;
  326.     short        item, menu;
  327.     short        daRefNum;
  328.     Rect        bounds;
  329.     Str255        daName;
  330.     WindowRef    newWindow;
  331.     
  332.     
  333.     menuChoice = *(long *)refCon;
  334.     
  335.     item = LoWord(menuChoice);
  336.     menu = HiWord(menuChoice);
  337.  
  338.     switch(menu) {
  339.         case mApple:
  340.             switch(item) {
  341.                 case iAbout:
  342.                     if ( !gHasAbout ) {
  343.                         SetRect( &bounds,2, 40 ,352 ,140 );
  344.                         newWindow = CreateWindow(nil, nil, &bounds, "\pAbout", true,
  345.                                                  documentProc, kAboutKind, (WindowPtr)-1,
  346.                                                  true, nil );
  347.                         gHasAbout = true;
  348.                     }                        
  349.                     break;
  350.                     
  351.                 default:
  352.                     GetItem(GetMHandle(mApple),item,daName);
  353.                     daRefNum = OpenDeskAcc( daName );
  354.                     break;
  355.             }    
  356.             break;
  357.                     
  358.         case mFile:
  359.             switch(item) {
  360.                 case iNew:
  361.                     SetRect(&bounds,100,100,300,300);
  362.                     newWindow = CreateWindow(128, nil, &bounds, nil, false,
  363.                                             documentProc, kDocKind, (WindowPtr)-1,
  364.                                             true, nil );
  365.                     break;
  366.                 case iOpen:
  367.                     DoOpenNew();
  368.                     break;
  369.                         
  370.                 case iClose:
  371.                     RemoveWindow(FrontWindow());
  372.                     break;    
  373.                         
  374.                 case iQuit:
  375.                     gDone = true;
  376.                     break;
  377.                     
  378.                 default:
  379.                     break;    
  380.             }
  381.             break;
  382.                     
  383.         default:
  384.             break;    
  385.         
  386.     }
  387.     
  388.     HiliteMenu(0);
  389.     
  390. }
  391.  
  392.  
  393. //----------------------------------------------------------------------
  394. //
  395. //    HandleContentClick - 
  396. //                
  397. //
  398. //----------------------------------------------------------------------
  399.  
  400. void HandleContentClick(WindowRef window, void *refCon)
  401. {
  402.     ControlRef            control;
  403.     ControlActionUPP    scrollUPP;
  404.     Point                mouse;
  405.     short                value;
  406.     short                thePart;
  407.     short                oldSetting;
  408.     short                horzScroll,vertScroll;
  409.     DocHnd                doc;
  410.  
  411.     doc = (DocHnd)GetWRefCon(window);
  412.     if (doc != nil) {
  413.         SetPort(window);
  414.         mouse = *(Point *)refCon;
  415.         GlobalToLocal(&mouse);
  416.         horzScroll = vertScroll = 0;
  417.  
  418.         if ((thePart = FindControl(mouse, window, &control)) != 0) {
  419.             switch(thePart) {
  420.                 case inThumb:
  421.                     oldSetting = GetCtlValue(control);
  422.                     thePart = TrackControl(control, mouse, 0L);
  423.                     if (thePart != 0) {
  424.                         value = oldSetting - GetCtlValue(control);
  425.                         if (value != 0){
  426.                             if (control == (**doc).hScroll)
  427.                                 horzScroll = value ;
  428.                             if (control == (**doc).vScroll)
  429.                                 vertScroll = value;
  430.                             MyScrollPicture(window, horzScroll, vertScroll);
  431.                         }
  432.                     }
  433.                     break;
  434.                 case inUpButton:
  435.                 case inDownButton:
  436.                 case inPageUp:
  437.                 case inPageDown:
  438.                     scrollUPP = NewControlActionProc(ScrollActionProc);
  439.                     value = TrackControl(control, mouse, scrollUPP);
  440.                     break;
  441.     
  442.             }
  443.         }
  444.     }
  445.  
  446. }
  447.  
  448.  
  449. //----------------------------------------------------------------------
  450. //
  451. //    HandleZoomClick - 
  452. //                
  453. //
  454. //----------------------------------------------------------------------
  455.  
  456. void HandleZoomClick(WindowRef window, void *refCon)
  457. {
  458.     short            part;
  459.     DocHnd            doc;
  460.     
  461.     doc = (DocHnd)GetWRefCon(window);
  462.     if (doc != nil) {
  463.         part = *(short *)refCon;
  464.         SetPort(window);
  465.         
  466.         EraseRect(&window->portRect);
  467.         ZoomWindow(window,part,true);
  468.         ClipRect(&window->portRect);
  469.         InvalRect(&window->portRect);
  470.         
  471.         HideControl((**doc).hScroll);
  472.         HideControl((**doc).vScroll);
  473.         
  474.         AdjustScrollbars(window, true);
  475.         
  476.         ShowControl((**doc).hScroll);
  477.         ShowControl((**doc).vScroll);
  478.         
  479.     }
  480. }
  481.  
  482.  
  483. //----------------------------------------------------------------------
  484. //
  485. //    HandleGrow - 
  486. //                
  487. //
  488. //----------------------------------------------------------------------
  489.  
  490. void HandleGrow(WindowRef window, void *refCon)
  491. {
  492.     Rect            gIRect;
  493.     Rect            limitRect;
  494.     long            growSize;
  495.         
  496.     limitRect = (**GetGrayRgn()).rgnBBox;
  497.     limitRect.left += 125;
  498.     limitRect.top += 125;
  499.  
  500.     growSize = GrowWindow(window, *(Point *)refCon, &limitRect);
  501.     if (growSize) {
  502.         SetPort(window);    
  503.         gIRect = window->portRect;
  504.         gIRect.top = gIRect.bottom - kScrollWidth;
  505.         gIRect.left = gIRect.right - kScrollWidth;     
  506.         EraseRect(&gIRect);
  507.         SizeWindow(window,LoWord(growSize),HiWord(growSize),true);
  508.         
  509.         ClipRect(&window->portRect);
  510.         AdjustScrollbars(window, true);
  511.         InvalRect(&window->portRect);
  512.     }
  513.  
  514. }
  515.  
  516.  
  517. //----------------------------------------------------------------------
  518. //
  519. //    UpdateWindow - update dispatcher for document windows.
  520. //                 
  521. //
  522. //----------------------------------------------------------------------
  523.  
  524. void UpdateWindow(WindowRef window) 
  525. {
  526.     GrafPtr        oldPort;
  527.     
  528.     
  529.     GetPort(&oldPort);
  530.     
  531.     SetPort(window);
  532.     BeginUpdate(window);
  533.     CustomWindowEvent(kUpdateProc, window, nil);
  534.     EndUpdate(window);
  535.     
  536.     SetPort(oldPort);
  537.  
  538. }
  539.  
  540.  
  541. //----------------------------------------------------------------------
  542. //
  543. //    DoActivate - 
  544. //                 
  545. //
  546. //----------------------------------------------------------------------
  547.  
  548. void DoActivate(WindowRef window, void *refCon)
  549. {
  550.     Boolean        becomingActive;
  551.     DocHnd        doc;
  552.     
  553.     SetPort(window);
  554.         
  555.     doc = (DocHnd)GetWRefCon(window);
  556.  
  557.     if(doc != nil && GetIsAppWindow(window)) {
  558.         becomingActive = *(Boolean *)refCon;
  559.         if (becomingActive) {
  560.             DrawGrowIcon(window);
  561.             ShowControl((**doc).hScroll);
  562.             ShowControl((**doc).vScroll);
  563.         }
  564.         else {
  565.             HideControl((**doc).hScroll);
  566.             HideControl((**doc).vScroll);
  567.             DrawGrowIcon(window);
  568.         }
  569.     }
  570.     
  571. }
  572.  
  573.